package com.yibo.redis;

import com.yibo.support.helper.JsonHelper;
import com.yibo.support.helper.ObjectHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

@Component
public class RedisCommand {

    private static final Logger LOGGER = LoggerFactory.getLogger(RedisCommand.class);


    @Resource
    private RedisTemplate redisTemplate;


    public boolean set(final String key,final String value){
        return (boolean) redisTemplate.execute((RedisCallback<Boolean>) redisConnection -> {
            RedisSerializer<String> serializer = redisSerializer();
            byte[] keyBytes = serializer.serialize(key);
            byte[] valueBytes = serializer.serialize(value);
            if(keyBytes == null || valueBytes == null){
                return false;
            }
            return  redisConnection.setNX(keyBytes,valueBytes);
        });
    }


    @SuppressWarnings("unchecked")
    public <T>T remove(final String key,final Class<T> clazz){
        return (T) redisTemplate.execute((RedisCallback<T>) redisConnection ->{
            RedisSerializer<String> serializer = redisSerializer();
            byte[] keys = serializer.serialize(key);
            byte[] value = redisConnection.get(keys);
            T result = null;
            if(value != null) {
                String valueStr = serializer.deserialize(value);
                result = JsonHelper.parseJson(valueStr,clazz);
                //删除
                redisConnection.del(keys);
            }
            return result;
        });
    }

    public boolean remove(final String key){
        return (boolean)this.redisTemplate.execute((RedisCallback) connection->{
            RedisSerializer<String> serializer = redisSerializer();
            byte[] bytes = serializer.serialize(key);
            Long ret = connection.del(bytes);
            return ret != null && ret == 1;
        });
    }



    @SuppressWarnings("unchecked")
    public long remove(final List<String> keys){

        if(ObjectHelper.isEmpty(keys)){
            return 0;
        }
        return redisTemplate.delete(keys);
    }


    @SuppressWarnings("unchecked")
    public  <T>T  get(final String key,final Class<T> clazz){
        return (T) redisTemplate.execute((RedisCallback<T>) connection->{
            RedisSerializer<String> serializer = redisSerializer();
            byte[] keyBytes = serializer.serialize(key);
            byte[] valueBytes = connection.get(keyBytes);
            if(valueBytes != null){
                String value = serializer.deserialize(valueBytes);
                return JsonHelper.parseJson(value,clazz);
            }
            return null;
        });
    }

    public Object  get(final String key){
        return  redisTemplate.execute((RedisCallback<Object>) connection->{
            RedisSerializer<String> serializer = redisSerializer();
            byte[] keyBytes = serializer.serialize(key);
            byte[] valueBytes = connection.get(keyBytes);
            if(valueBytes != null){
                return serializer.deserialize(valueBytes);
            }
            return null;
        });
    }


    private RedisSerializer<String> redisSerializer(){
        return this.redisTemplate.getStringSerializer();
    }


}
